package com.hyz.service.impl;

import client.AssetClient;
import client.LogClient;
import com.alibaba.fastjson.JSON;
import com.hyz.pojo.ResponseResult;
import com.hyz.pojo.repositoryManager.GetAssetImportsResponse;
import com.hyz.pojo.repositoryManager.GetAssetLogByAssetIdResponse;
import com.hyz.pojo.repositoryManager.UpdateAssetImportsRequest;
import com.hyz.service.RepositoryManagerService;
import com.hyz.util.GetValue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.asset.*;
import pojo.log.assetImport.AssetImport;
import pojo.log.assetImport.AssetImportResultEnum;
import pojo.log.assetLog.AssetLog;
import pojo.log.IsComplete;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HGD
 * @date 2022/10/4 0:47
 */
@Service
public class RepositoryManagerServiceImpl implements RepositoryManagerService {
    @Autowired
    private AssetClient assetClient;
    @Autowired
    private LogClient logClient;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private GetValue getValue;

    @Override
    public ResponseResult<List<GetAssetImportsResponse>> getAssetImports() {
        List<GetAssetImportsResponse> responses = new ArrayList<>();
        // TODO: 获取仓库管理员id
        Long repositoryMangerId = 1L;
        List<RepositoryCharge> repositoryCharges = assetClient.getRepositoryChargeByRepositoryManagerId(repositoryMangerId);
        for (RepositoryCharge repositoryCharge : repositoryCharges) {
            Long repositoryId = repositoryCharge.getRepositoryId();
            List<AssetImport> assetImports = logClient.getAssetImportsByRepositoryId(repositoryId);
            for (AssetImport assetImport : assetImports) {
                GetAssetImportsResponse response = new GetAssetImportsResponse();
                response.setAssetId(assetImport.getAssetId());
                Asset asset = assetClient.getAssetById(assetImport.getAssetId());
                response.setAssetName(asset.getAssetName());
                response.setRepositoryId(assetImport.getRepositoryId());
                response.setAssetManagerId(assetImport.getAssetManagerId());
                response.setCreateTime(assetImport.getCreateTime().toString());
                response.setResult(assetImport.getResult().getValue());
                responses.add(response);
            }
        }
        return new ResponseResult<>(200, "成功", responses);
    }

    @Override
    public ResponseResult<Boolean> batchUpdateAssetImports(UpdateAssetImportsRequest request) {
        List<Long> assetIds = request.getAssetIds();
        Long repositoryId = request.getRepositoryId();
        Boolean result = request.getResult();
        if (assetIds.isEmpty() || repositoryId == null || result == null) {
            return new ResponseResult<>(500, "参数错误", false);
        }

        // TODO: 获取用户职责
        String right = "repositoryManager";


        // 1. 入库申请更新
        List<AssetImport> assetImports = new ArrayList<>();
        // 2. 资产日志创建
        List<AssetLog> assetLogs = new ArrayList<>();
        // 3. 资产属性修改
        List<Asset> assets = new ArrayList<>();
        // 4. 资产-仓库创建
        List<RepositoryAsset> repositoryAssets = new ArrayList<>();

        for (Long assetId : assetIds) {
            AssetImport assetImport = new AssetImport();
            assetImport.setAssetId(assetId);
            assetImport.setResult(result ? AssetImportResultEnum.SUCCEEDED : AssetImportResultEnum.FAILED);
            assetImport.setIsComplete(IsComplete.COMPLETE);
            assetImports.add(assetImport);

            AssetLog assetLog = new AssetLog();
            assetLog.setId(getValue.getUuid());
            assetLog.setAssetId(assetId);
            assetLog.setRepositoryId(repositoryId);
            assetLog.setResult(result ? AssetImportResultEnum.SUCCEEDED.getValue() : AssetImportResultEnum.FAILED.getValue());
            assetLogs.add(assetLog);

            Asset asset = new Asset();
            asset.setId(assetId);
            asset.setStatus(AssetStatusEnum.UNLOCK);
            assets.add(asset);

            RepositoryAsset repositoryAsset = new RepositoryAsset();
            repositoryAsset.setId(getValue.getUuid());
            repositoryAsset.setRepositoryId(repositoryId);
            repositoryAsset.setAssetId(assetId);
            repositoryAssets.add(repositoryAsset);
        }

        String assetImportsJson = JSON.toJSONString(assetImports);
        String assetLogsJson = JSON.toJSONString(assetLogs);
        String assetsJson = JSON.toJSONString(assets);
        String repositoryAssetsJson = JSON.toJSONString(repositoryAssets);

        rabbitTemplate.convertAndSend(right, "batchUpdateAssetImports", assetImportsJson);
        rabbitTemplate.convertAndSend("batchInsertAssetLogs", null, assetLogsJson);
        rabbitTemplate.convertAndSend(right, "batchUpdateAssets", assetsJson);
        rabbitTemplate.convertAndSend(right, "batchInsertRepositoryAssets", repositoryAssetsJson);
        return new ResponseResult<>(200, "成功", true);
    }

    @Override
    public ResponseResult<List<GetAssetLogByAssetIdResponse>> getAssetLogsByAssetId(Long assetId) {
        if (assetId == null) {
            return new ResponseResult<>(500, "参数错误");
        }
        List<AssetLog> assetLogs = logClient.getAssetLogsByAssetId(assetId);
        List<GetAssetLogByAssetIdResponse> responses = new ArrayList<>();
        for (AssetLog assetLog : assetLogs) {
            GetAssetLogByAssetIdResponse response = new GetAssetLogByAssetIdResponse();
            response.setAssetId(assetLog.getAssetId());
            Asset asset = assetClient.getAssetById(assetLog.getAssetId());
            response.setAssetName(asset.getAssetName());
            response.setRepositoryId(assetLog.getRepositoryId());
            if (assetLog.getRepositoryId() != null) {
                Repository repository = assetClient.getRepositoryById(assetLog.getRepositoryId());
                response.setRepositoryName(repository.getRepositoryName());
            }
            response.setCreateTime(assetLog.getCreateTime().toString());
            response.setResult(assetLog.getResult());
            responses.add(response);
        }
        return new ResponseResult<>(200, "成功", responses);
    }
}
